package com.example.common.tab

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.example.common.R
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem

/**
 * 带圆角的Tab
 * */
class RoundTabView :BaseTabItem{

    private lateinit var mIcon : ImageView
    private lateinit var mTitle : TextView
    private lateinit var mMsg : RoundMessageView

    private lateinit var mDefaultIcon : Drawable
    private lateinit var mCheckedIcon : Drawable
    private lateinit var mTabName : String
    private  var mChecked :Boolean =false
    private var mMsgNumber : Int = 0

    private var mDefaultTextColor : Int=0X56000000
    private var mCheckedTextColor : Int=0X56000000

    constructor(context: Context): super(context){
        initViews(context)
    }

    constructor(context: Context,attrs:AttributeSet?): super(context,attrs){
        initViews(context)
    }
    constructor(context: Context,attrs: AttributeSet?,defaultAttr : Int):super(context, attrs,defaultAttr){

        initViews(context)


    }
    private fun initViews(context: Context) {
        //加载UI布局
        LayoutInflater.from(context).inflate(R.layout.type_round_layout,this,true)
        //初始化组件
        mIcon=findViewById(R.id.tab_icon)
        mTitle=findViewById(R.id.tab_title)
        mMsg=findViewById(R.id.tab_msg)
    }
    /**
     * 初始化资源数据
     * */
    fun initData(@DrawableRes defaultIcon :Int,@DrawableRes checkIcon :Int,title: String,hasMeg : Boolean=false,msgNumber: Int=0){
        //获取图标
        mDefaultIcon= ContextCompat.getDrawable(context,defaultIcon)!!
        mCheckedIcon=ContextCompat.getDrawable(context,checkIcon)!!

        mChecked=hasMeg
        mTabName=title
        mMsgNumber=msgNumber
        mTitle.text=mTabName
    }

    override fun setChecked(checked: Boolean) {
        if (checked){
            mIcon.setImageDrawable(mCheckedIcon)
            mTitle.setTextColor(mCheckedTextColor)
        }else{
            mIcon.setImageDrawable(mDefaultIcon)
            mTitle.setTextColor(mDefaultTextColor)
        }
        mChecked=checked
    }

    override fun setMessageNumber(number: Int) {
        mMsg.messageNumber=number
    }

    override fun setHasMessage(hasMessage: Boolean) {
       mMsg.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String?) {
        mTitle.text=title
    }

    override fun setDefaultDrawable(drawable: Drawable?) {
        if (drawable != null) {
            mDefaultIcon = drawable
        }
        if (!mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }

    override fun setSelectedDrawable(drawable: Drawable?) {
        if (drawable != null) {
            mCheckedIcon=drawable
        }

        if (mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }
    override fun getTitle(): String {
       return mTitle.text.toString()
    }

    /**
     * 设置默认颜色值
     * */
    fun setDefaultTextColor(@ColorRes colorResId : Int){
        mDefaultTextColor=ContextCompat.getColor(context,colorResId)
    }

    fun setSelectedTextColor(@ColorRes colorResId : Int){
        mCheckedTextColor=ContextCompat.getColor(context,colorResId)
    }

}